//
//  ResultViewController.m
//  QrcodeScanner
//
//  Created by Nguyen Cong Huy on 9/12/14.
//  Copyright (c) 2014 FreeAppForYou. All rights reserved.
//

#import "ResultViewController.h"

#define kBottomBarHeight 44

@interface ResultViewController () {
    UITextView* contentTextView;
}

@end

@implementation ResultViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        int padding = 5;
        
        contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(padding, 0, self.view.width - 2*padding, self.view.height - kBottomBarHeight)];
        contentTextView.text = self.content;
        contentTextView.font = [UIFont systemFontOfSize:18];
        contentTextView.editable = NO;
        [self.view addSubview:contentTextView];
        
        UIView* bottomBar = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height - kBottomBarHeight, self.view.width, kBottomBarHeight)];
        bottomBar.backgroundColor = RGBCOLOR(236, 243, 249);
        [self.view addSubview:bottomBar];
        
        UIButton* copyButton = [UIButton buttonWithType:UIButtonTypeSystem];
        copyButton.frame = CGRectMake(0, 0, bottomBar.width, bottomBar.height);
        [copyButton setTitle:NSLocalizedString(@"Copy", nil) forState:UIControlStateNormal];
        copyButton.titleLabel.font = [UIFont systemFontOfSize:20];
        [copyButton addTarget:self action:@selector(copyButtonTouched) forControlEvents:UIControlEventTouchUpInside];
        [bottomBar addSubview:copyButton];
        
        if([self.content hasPrefix:@"http://"] || [self.content hasPrefix:@"https://"]
           || [self.content hasPrefix:@"www."]) {
            copyButton.frame = CGRectMake(0, 0, bottomBar.width/2, bottomBar.height);
            
            UIButton* openLinkButton = [UIButton buttonWithType:UIButtonTypeSystem];
            openLinkButton.frame = CGRectMake(copyButton.right, 0, bottomBar.width/2, bottomBar.height);
            [openLinkButton setTitle:NSLocalizedString(@"Open link", nil) forState:UIControlStateNormal];
            openLinkButton.titleLabel.font = [UIFont systemFontOfSize:20];
            [openLinkButton addTarget:self action:@selector(openLinkButtonTouched) forControlEvents:UIControlEventTouchUpInside];
            [bottomBar addSubview:openLinkButton];
        }
    }];

}

- (void)copyButtonTouched {
    [self.view makeToast:NSLocalizedString(@"Copied", nil) duration:3 position:@"top"];
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = contentTextView.text;
}

- (void)openLinkButtonTouched {
    if ([self.content hasPrefix:@"www."]) {
        self.content = [NSString stringWithFormat:@"%@%@", @"http://", self.content];
    }
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.content]];
}

@end
